package com.iou.view;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.iou.bean.IOUNote;
import com.iou.bean.IOUUser;
import com.iou.database.DatabaseHelper;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Notification;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Reindeer;

/**
 * Window that opens when a note is double-clicked on the ListView. It provides
 * abilities to check, edit, and delete the selected note.
 * 
 */
public class InfoView {
    /*
     * Class variables
     */
    private Window mywindow;
    private Button savebutton;
    private Button editbutton;
    private Button deletebutton;
    private CheckBox Status;
    private IOUUser user;
    final TextField participant;
    final OptionGroup noteSelect;
    final TextField amount;
    final PopupDateField date;
    final TextArea description;
    private boolean status;
    private IOUNote note;

    private static final List<String> types = Arrays.asList(new String[] {
            "IOU", "UOMe" });

    /**
     * Constructor.
     * 
     * @param user
     * @param note
     */
    public InfoView(final IOUUser user, final IOUNote note) {
        this.note = note;
        this.user = user;
        AbsoluteLayout windowLayout = new AbsoluteLayout();
        windowLayout.setSizeFull();
        mywindow = new Window("IOU");
        mywindow.center();
        mywindow.setHeight("405px");
        mywindow.setWidth("380px");
        mywindow.setResizable(false);
        mywindow.setModal(true);
        mywindow.setContent(windowLayout);

        UI.getCurrent().addWindow(mywindow);

        /*
         * Componenents initialization
         */
        savebutton = new Button("Save");
        editbutton = new Button("Edit");
        deletebutton = new Button("Delete");
        deletebutton.addStyleName(Reindeer.BUTTON_SMALL);
        Status = new CheckBox("Complete?");

        noteSelect = new OptionGroup("Type:", types);

        amount = new TextField("Amount:");
        amount.setWidth("50px");

        participant = new TextField("Participant:");
        participant.setWidth("200px");

        date = new PopupDateField("Date:", new Date(System.currentTimeMillis()));

        description = new TextArea("Description:");
        description.setWidth("300px");

        /*
         * Save button. Updates a note with entered values and refresh table.
         */
        savebutton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                if (Status.getValue() == true) {
                    status = true;
                } else {
                    status = false;
                }
                IOUNote newNote = new IOUNote();
                try {
                    newNote = new IOUNote(noteSelect.getValue().toString(),
                            Double.parseDouble(amount.getValue()), participant
                                    .getValue(), description.getValue(), date
                                    .getValue().getDate(), (date.getValue()
                                    .getMonth() + 1), (date.getValue()
                                    .getYear() + 1900), status);

                    newNote.setUser(user);
                    newNote.setId(note.getId());
                    DatabaseHelper.storeNote(newNote);
                    Notification.show("Note updated!",
                            Notification.Type.TRAY_NOTIFICATION);
                    mywindow.close();
                    ListView.refreshList();
                    ListView.addToTable();
                } catch (NumberFormatException e) {
                    Notification.show("Invalid amount!",
                            "Please enter a valid amount, like 3, 256.6 or 0.",
                            Notification.Type.ERROR_MESSAGE);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        getInfo();
        /*
         * Edit button. Disables components readOnly function.
         */
        editbutton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                noteSelect.setReadOnly(false);
                amount.setReadOnly(false);
                participant.setReadOnly(false);
                date.setReadOnly(false);
                description.setReadOnly(false);
                Status.setReadOnly(false);
            }
        });

        /*
         * Delete button. Opens a new DeleteView window..
         */
        final InfoView thisInfoViewWindow = this;
        deletebutton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                DeleteView deleteWindow = new DeleteView(user, note,
                        thisInfoViewWindow);
            }
        });

        /*
         * Addition/alignment
         */
        windowLayout.addComponent(Status, "bottom:13px; left:150px");
        windowLayout.addComponent(noteSelect, "top:25px; left:10px");
        windowLayout.addComponent(amount, "top:85px; left:10px");
        windowLayout.addComponent(participant, "top:135px; left:10px");
        windowLayout.addComponent(date, "top:185px; left:10px");
        windowLayout.addComponent(description, "top:235px; left:10px");
        windowLayout.addComponent(savebutton, "bottom:10px; right:10px");
        windowLayout.addComponent(editbutton, "bottom:10px; right:80px");
        windowLayout.addComponent(deletebutton, "bottom:10px; left:10px");
    }

    /**
     * Retrieves information from note to InfoView and sets components readOnly
     * value to true.
     */
    @SuppressWarnings("deprecation")
    public void getInfo() {
        participant.setValue(note.getParticipant());
        description.setValue(note.getDescription());
        amount.setValue(String.valueOf(note.getAmount()));
        if (note.getStatus() == true) {
            Status.setValue(true);
        } else {
            Status.setValue(false);
        }
        if (note.getType() == "IOU") {
            noteSelect.select(types.get(0));
        } else {
            noteSelect.select(types.get(1));
        }
        date.getValue().setDate(note.getDay());
        date.getValue().setMonth((note.getMonth() - 1));
        date.getValue().setYear((note.getYear() - 1900));
        date.setDateFormat("d.M.yyy");
        Status.setReadOnly(true);
        description.setReadOnly(true);
        date.setReadOnly(true);
        participant.setReadOnly(true);
        amount.setReadOnly(true);
        noteSelect.setReadOnly(true);
    }

    /**
     * Method that closes the window.
     */
    public void closeWIndow() {
        mywindow.close();
    }
}